<template>
	<view class="container">
		<view class="content-box">
			<view class="title">{{newsInfo.title}}</view>
			<view class="intro-text">
				<u-parse className="rich-img" :content="newsInfo.text"></u-parse>
			</view>
			<!-- #ifdef MP -->
			<view class="file-list" v-if="fileArr.length > 0">
				<view v-for="item in fileArr" class="item" @click="download(item.url)">{{item.name}}</view>
			</view>
			<!-- #endif -->
			<view class="footer-cont">
				<footer-cont></footer-cont>
			</view>
		</view>
	</view>
</template>

<script>
	import uParse from '@/components/u-parse/u-parse.vue';
	import footerCont from '@/components/footer-module.vue';
	export default {
		components: {
			uParse,
			footerCont
		},
		data() {
			return {
				newsId:'',
				newsInfo:{},
				fileArr:[]
			};
		},
		onShow() {
			
		},
		onLoad(options) {
			const that = this
			that.newsId = options.id
			this.loadData()
		},
		onShareAppMessage() {
			const that = this
			return {
				title: '秋歌体验田',
				imageUrl: 'http://shopmalling-asset.oss-cn-hangzhou.aliyuncs.com/share_bg.jpg',
				path: '/pages/index/index'
			}
		},
		methods: {
			async loadData() {
				const that = this
				uni.showLoading({
					title: '正在加载'
				})
				that.$api.request('news', 'detail',{id:that.newsId}, failres => {
					that.$api.msg(failres.errmsg)
					uni.hideLoading()
				}).then(res => {
					let data = res.data
					const regex = new RegExp('<img','gi');
					const regex_p = new RegExp('<p','gi');
					data.text = data.text.replace(regex,'<img style="max-width:100%;height:auto;"');
					data.text = data.text.replace(regex_p,'<p style="margin-top:32upx;"');
					that.newsInfo = data
					if(data.fileUrl){
						console.log(data)
						let fileurl = data.fileUrl.split(",")
						let filename = data.fileName.split(",")
						let arr = []
						fileurl.forEach((item,index) => {
							arr.push({
								name:filename[index],
								url:item
							})
						})
						that.fileArr = arr
					}
					uni.hideLoading()
				})
			},
			download(url){
				uni.downloadFile({
				    url: url, //仅为示例，并非真实的资源
				    success: (res) => {
				        if (res.statusCode === 200) {
							console.log(res)
							const filePath = res.tempFilePath
							uni.openDocument({
							  filePath: filePath,
							  showMenu:true,
							  success: function (res) {
								
							  },
							  fail:function(res){
								  uni.showToast({
								  	icon:'none',
								      title: '打开文档失败，格式不支持',
								      duration: 2000
								  })
							  }
							})
				        }
				    }
				});
			}
		},
	}
</script>

<style lang="less" scoped>
	@import 'commonStyle.less';
	.container{
		position: relative;
		padding-bottom:30upx;
	}
	.title{
		padding:40upx 60upx 0;
		font-size: 36upx;
		color: #333333;
		font-weight: bold;
		line-height: 40upx;
	}
	.intro-text{
		margin-top: 27upx;
		padding:0 40upx;
		.rich-img{
			font-size: 28upx;
			color: #666666;
			text-align: justify;
			line-height: 40upx;
		}
	}
	.file-list{
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		margin-top:30upx;
		padding:0 40upx;
		font-size: 28upx;
		.item{
			padding-bottom:5upx;
			color:#0DB7E2;
			border-bottom:2upx solid #0DB7E2;
			&:not(:first-child){
				margin-left:10upx;
			}
		}
	}
	.footer-cont{
		margin: 64upx 40upx 40upx 40upx;
		box-shadow:0px 3upx 29upx 0px rgba(196, 196, 196, 0.35);
	}
</style>
